package com.xingdian.xingdianhulianserver.web;

import com.xingdian.xingdianhulianserver.common.QueryObject;
import com.xingdian.xingdianhulianserver.common.ServerResponse;
import com.xingdian.xingdianhulianserver.service.IProjectJoin;
import com.xingdian.xingdianhulianserver.util.ossUtil.UploadUtil;
import com.xingdian.xingdianhulianserver.vo.IpInfo;
import com.xingdian.xingdianhulianserver.vo.ProjectJoin;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;
import java.util.Map;

/* *
 * @Author H2SO4
 * @ClassName: ProjectJoinController
 * @Description TODO
 * @Date 2019/12/27--10:04
 * */
@Controller
public class ProjectJoinController {

    @Autowired
    IProjectJoin iProjectJoin;


    //跳转页面
    @GetMapping("toprojectjoin")
    public String toprojectjoin(){
        return "projectjoin";
    }
    //跳转页面
    @GetMapping("toaddprojectjoin")
    public String toaddprojectjoin(){
        return "addprojectjoin";
    }

    //查询需要修改的信息   并跳转到修改页面
    @GetMapping("/toupdateprojectjoin")
    public String toupdateprojectjoin(Model model,Integer id,HttpServletResponse response){
        ProjectJoin projectJoin = iProjectJoin.findById(id);
        model.addAttribute("projectJoin",projectJoin);
        Cookie cookie = new Cookie("pid","-1");
        //设置Cookie的最大生命周期,否则浏览器关闭后Cookie即失效
        cookie.setMaxAge(Integer.MAX_VALUE);
        //将Cookie加到response中
        response.addCookie(cookie);
        return "updateprojectjoin";
    }

    //修改
    @PostMapping("/updateprojectjoin")
    public String updateprojectjoin(ProjectJoin projectJoin){
        iProjectJoin.updateprojectjoin(projectJoin);
        return "projectjoin";
    }

    @GetMapping("delprojectjoin")
    public String delprojectjoin(Integer id){
        iProjectJoin.delprojectjoin(id);
        return "projectjoin";
    }

    //修改主图
    @PostMapping("upload11")
    public String uolpadImage11(HttpServletRequest request,HttpServletResponse response,MultipartFile img,Model model){
        String str = UploadUtil.upload4(img);
        Cookie cookie = new Cookie("imgUrl",str);
        //设置Cookie的最大生命周期,否则浏览器关闭后Cookie即失效
        cookie.setMaxAge(Integer.MAX_VALUE);
        //将Cookie加到response中
        response.addCookie(cookie);
        Cookie[] cookies = request.getCookies();
        int id = 0;
        if(cookies != null && cookies.length > 0) {
            for (Cookie c : cookies) {
                if (c.getName().equals("pid")) {
                    id = Integer.valueOf(c.getValue());
                }
            }
        }
        ProjectJoin projectJoin = iProjectJoin.findById(id);
        model.addAttribute("projectJoin",projectJoin);
        return "updateprojectjoin";
    }

    //修改项目图s
    @PostMapping("upload12")
    public String uolpadImage12(HttpServletRequest request,HttpServletResponse response,MultipartFile img,Model model){
        String str = UploadUtil.upload2(img);
        Cookie cookie = new Cookie("str2",str);
        //设置Cookie的最大生命周期,否则浏览器关闭后Cookie即失效
        cookie.setMaxAge(Integer.MAX_VALUE);
        //将Cookie加到response中
        response.addCookie(cookie);
        Cookie[] cookies = request.getCookies();
        int id = 0;
        if(cookies != null && cookies.length > 0) {
            for (Cookie c : cookies) {
                if (c.getName().equals("pid")) {
                    id = Integer.valueOf(c.getValue());
                }
            }
        }
        ProjectJoin projectJoin = iProjectJoin.findById(id);
        model.addAttribute("projectJoin",projectJoin);
        return "updateprojectjoin";
    }

    //修改详情图片
    @PostMapping("upload13")
    public String uolpadImage13(HttpServletRequest request,HttpServletResponse response,MultipartFile img,Model model){
        String str = UploadUtil.upload3(img);
        Cookie cookie = new Cookie("str",str);
        //设置Cookie的最大生命周期,否则浏览器关闭后Cookie即失效
        cookie.setMaxAge(Integer.MAX_VALUE);
        //将Cookie加到response中
        response.addCookie(cookie);
        Cookie[] cookies = request.getCookies();
        int id = 0;
        if(cookies != null && cookies.length > 0) {
            for (Cookie c : cookies) {
                if (c.getName().equals("pid")) {
                    id = Integer.valueOf(c.getValue());
                }
            }
        }
        ProjectJoin projectJoin = iProjectJoin.findById(id);
        model.addAttribute("projectJoin",projectJoin);
        return "updateprojectjoin";
    }



    //异步加载页面信息
    @GetMapping("/findProjectById")
    @ResponseBody
    public ServerResponse findProjectById(@RequestParam("id") Integer id,HttpServletResponse response,HttpServletRequest request){
        Map<String,Object> map = iProjectJoin.findList(id);
        int pid = 0;
        Cookie[] cookies = request.getCookies();
        if(cookies != null && cookies.length > 0) {
            for (Cookie c : cookies) {
                if (c.getName().equals("pid")) {
                    pid = Integer.valueOf(c.getValue());
                }
            }
        }
        if(pid==-1){
                Cookie cookie1 = new Cookie("str",null);
                cookie1.setMaxAge(0);
                Cookie cookie2 = new Cookie("str2",null);
                cookie2.setMaxAge(0);
                ProjectJoin projectJoin = (ProjectJoin) map.get("projectJoin");
                Cookie cookie6 = new Cookie("pid",URLEncoder.encode(projectJoin.getId().toString()));
                cookie6.setMaxAge(Integer.MAX_VALUE);
                response.addCookie(cookie6);
                if(projectJoin.getImgUrl()!=null){
                    Cookie cookie3 = new Cookie("imgUrl",URLEncoder.encode(projectJoin.getImgUrl()));
                    cookie3.setMaxAge(Integer.MAX_VALUE);
                    response.addCookie(cookie3);
                }else {
                    Cookie cookie3 = new Cookie("str",null);
                    cookie3.setMaxAge(0);
                    response.addCookie(cookie3);
                }
                if(projectJoin.getDetailImg()!=null){
                    Cookie cookie4 = new Cookie("projectImg", URLEncoder.encode(projectJoin.getProjectImg()));
                    cookie4.setMaxAge(Integer.MAX_VALUE);
                    response.addCookie(cookie4);
                }else {
                    Cookie cookie4 = new Cookie("projectImg",null);
                    cookie4.setMaxAge(0);
                    response.addCookie(cookie4);
                }
                if(projectJoin.getProjectImg()!=null){
                    Cookie cookie5 = new Cookie("detailImg",URLEncoder.encode(projectJoin.getDetailImg()));
                    cookie5.setMaxAge(Integer.MAX_VALUE);
                    response.addCookie(cookie5);
                }else {
                    Cookie cookie5 = new Cookie("detailImg",null);
                    cookie5.setMaxAge(0);
                    response.addCookie(cookie5);
                }

                response.addCookie(cookie1);
                response.addCookie(cookie2);
        }
        System.out.println(id);
        return ServerResponse.createBySuccess(map);
    }


    //查询数据
    @PostMapping("findprojectjoin")
    @ResponseBody
    public ServerResponse findprojectjoin(QueryObject queryObject){
        return iProjectJoin.findprojectjoin(queryObject);
    }


    //加盟项目图s
    @PostMapping("upload2")
    public String uolpadImage2(HttpServletResponse response,MultipartFile img){
        String str = UploadUtil.upload2(img);
        Cookie cookie = new Cookie("str2",str);
        //设置Cookie的最大生命周期,否则浏览器关闭后Cookie即失效
        cookie.setMaxAge(Integer.MAX_VALUE);
        //将Cookie加到response中
        response.addCookie(cookie);
        return "addprojectjoin";
    }

    //上传主图
    @PostMapping("upload4")
    public String uolpadImage4(HttpServletResponse response,MultipartFile img){
        String str = UploadUtil.upload4(img);
        Cookie cookie = new Cookie("imgUrl",str);
        //设置Cookie的最大生命周期,否则浏览器关闭后Cookie即失效
        cookie.setMaxAge(Integer.MAX_VALUE);
        //将Cookie加到response中
        response.addCookie(cookie);
        return "addprojectjoin";
    }

    //上传详情图片
    @PostMapping("upload3")
    public String uolpadImage3(HttpServletResponse response,MultipartFile img){
        String str = UploadUtil.upload3(img);
/*        Cookie[] cookies = request.getCookies();
        if(cookies != null && cookies.length > 0) {
            for (Cookie cookie : cookies) {
                if (cookie.getName().equals("detailImg")) {
                    str = cookie.getValue()+","+UploadUtil.upload(img);
                }
            }
        }*/

        //String uploadImageKey = UploadUtil.upload(img);
        Cookie cookie = new Cookie("str",str);
        //设置Cookie的最大生命周期,否则浏览器关闭后Cookie即失效
        cookie.setMaxAge(Integer.MAX_VALUE);
        //将Cookie加到response中
        response.addCookie(cookie);
        //model.addAttribute("uploadImageKey",uploadImageKey);
        return "addprojectjoin";
    }

    //页面跳转
    @RequestMapping(value="/toSjqnDetail")
    public String toSjqnDetail(Integer id, HttpServletResponse response){
        //创建Cookie
        Cookie cookie = new Cookie("id",id.toString());
        //设置Cookie的最大生命周期,否则浏览器关闭后Cookie即失效
        cookie.setMaxAge(Integer.MAX_VALUE);
        //将Cookie加到response中
        response.addCookie(cookie);
        System.out.println("=================================");
        return  "projectdetail";
    }


    //保存数据
    @PostMapping("addprojectjoin")
    public String addprojectjoin(ProjectJoin projectJoin){
        int count = iProjectJoin.addprojectjoin(projectJoin);
        if(count>0){
            return "projectjoin";
        }
        return "addprojectjoin";
    }

    @ResponseBody
    @GetMapping("findLists")
    public ServerResponse findLists(){
        return iProjectJoin.findLists();
    }

    //查寻城市下拉列表
    @ResponseBody
    @GetMapping("findCitys")
    public ServerResponse findCitys(){
        return iProjectJoin.findCitys();
    }

    private final static byte[] val = {
            0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,
            0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,
            0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,
            0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,
            0x3F,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,
            0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,
            0x3F,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,
            0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,
            0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,
            0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,
            0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,
            0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,
            0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,
            0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,
            0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,
            0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F
    };
    //解密cookie值
    public static String unescape(String s) {
        StringBuffer sbuf = new StringBuffer();
        int i = 0;
        int len = s.length();
        while (i < len) {
            int ch = s.charAt(i);
            if (ch == '+') {                        // + : map to ' '
                sbuf.append(' ');
            } else if ('A' <= ch && ch <= 'Z') {    // 'A'..'Z' : as it was
                sbuf.append((char)ch);
            } else if ('a' <= ch && ch <= 'z') {    // 'a'..'z' : as it was
                sbuf.append((char)ch);
            } else if ('0' <= ch && ch <= '9') {    // '0'..'9' : as it was
                sbuf.append((char)ch);
            } else if (ch == '-' || ch == '_'       // unreserved : as it was
                    || ch == '.' || ch == '!'
                    || ch == '~' || ch == '*'
                    || ch == '/' || ch == '('
                    || ch == ')' || ch == '{'
                    || ch == '}' || ch == '['
                    || ch == ']' || ch == ':'
                    || ch == '"' || ch == ','
                    || ch == ' ') {
                sbuf.append((char)ch);
            } else if (ch == '%') {
                int cint = 0;
                if ('u' != s.charAt(i+1)) {         // %XX : map to ascii(XX)
                    cint = (cint << 4) | val[s.charAt(i+1)];
                    cint = (cint << 4) | val[s.charAt(i+2)];
                    i+=2;
                } else {                            // %uXXXX : map to unicode(XXXX)
                    cint = (cint << 4) | val[s.charAt(i+2)];
                    cint = (cint << 4) | val[s.charAt(i+3)];
                    cint = (cint << 4) | val[s.charAt(i+4)];
                    cint = (cint << 4) | val[s.charAt(i+5)];
                    i+=5;
                }
                sbuf.append((char)cint);
            }
            i++;
        }
        return sbuf.toString();
    }

/*    //修改图片
    @PostMapping("toupload")
    public String uolpadImage(MultipartFile img, Integer id1, RedirectAttributes redirectAttributes){
        String uploadImageKey = UploadUtil.upload(img);
        IpInfo ipInfo = new IpInfo();
        ipInfo.setId(id1);
        ipInfo.setImgUrl(uploadImageKey);
        ipInfoService.updateIpInfo(ipInfo);
        //传递参数
        //redirectAttributes.addFlashAttribute("id",id1);
        return "redirect:/updateInfo?id="+id1;
    }

    //保存数据
    @PostMapping("addprojectjoin")
    public String addprojectjoin(Model model, IpInfo ipInfo){
        int count = ipInfoService.add(ipInfo);
        if(count>0){
            return "home";
        }
        return "add";
    }

    //查询需要修改的信息   并跳转到修改页面
    @PostMapping("/update")
    public String update(Model model,IpInfo ipInfo){
        ipInfoService.updateIpInfo(ipInfo);
        return "home";
    }

    //查询需要修改的信息   并跳转到修改页面
    @GetMapping("/updateInfo")
    public String toupdateInfo(Model model, Integer id){
        IpInfo ipInfo = ipInfoService.findById(id);
        model.addAttribute("ipInfo",ipInfo);
        return "update";
    }

    //跳转添加页面
    @GetMapping("toadd")
    public String toAdd(){
        return "add";
    }

    //跳转页面
    @GetMapping("toindex")
    public String toindex(){
        return "index";
    }

    //跳转页面
    @GetMapping("tomain")
    public String tomain(){
        return "main";
    }

    //跳转页面
    @GetMapping("tohome")
    public String tohome(){
        return "home";
    }

    //跳转页面
    @GetMapping("tonav")
    public String tonav(){
        return "nav";
    }

    //查询数据
    @PostMapping("findinfo")
    @ResponseBody
    public ServerResponse findinfo(QueryObject queryObject){
        return ipInfoService.findinfo(queryObject);
    }

    //删除
    @GetMapping("delInfo")
    public String delInfo(Integer id){
        ipInfoService.delInfo(id);
        return "home";
    }*/

}
